import numpy as np
from PyQt4 import QtCore
import sys,os,inspect
sys.path.insert(0,os.path.abspath(os.path.join(os.getcwd(), os.pardir)))## add root path 
from KModel.KSingleStock import KSingleStock
from KModel.KSingleStockDataProcessor import KSingleStockDataProcessor as kprocesser
from KModel.KSingleStockCursor import KSingleStockCursor 
from KTrade.KStockFeed import KStockFeed
from KTrade.KPosition import KPosition

class KBaseMetric(QtCore.QObject):
	def __init__(self,parent):
		assert(isinstance(parent,KPosition)==True)
		QtCore.QObject.__init__(self,parent)
		self.position=parent # make sure the parent is KPosition

	def UpdateOnNewBars(self):
	# update the tracking when new bars are generated
		pass
	def GetMetricData(self):
		pass

class KMetricProfit(KBaseMetric):
	# calcuate trades profict
	def __init__(self,parent):
		KBaseMetric.__init__(self,parent)
		self.profit=[] # record all historical returns

		
	def UpdateOnNewBars(self):
	# update the tracking when new bars are generated
		pass

		
			
	def GetMetricData(self):
	# return data in terms of dict
		# Calculate Sharp Ratio
		profit=self.position.GetCurrentProfit()
		
		Metrics={}
		Metrics['Profit']=profit
		
		return Metrics	
	
class KMetricTrades(KBaseMetric):
	# calcuate trades information
	def __init__(self,parent):
		KBaseMetric.__init__(self,parent)
		self.returns=[] # record all historical returns
		self.sharpratio=0	
		
	def UpdateOnNewBars(self):
	# update the tracking when new bars are generated
		pass

		
			
	def GetMetricData(self):
	# return data in terms of dict
		# Calculate Sharp Ratio
		orderlist=self.position.GetAllOrders()
		
		Metrics={}
		Metrics['Trades Count']=len(orderlist)
		
		return Metrics
	
class KMetricSharpRatio(KBaseMetric):
	# calcuate sharp ratio
	def __init__(self,parent):
		KBaseMetric.__init__(self,parent)
		self.returns=[] # record all historical returns
		self.sharpratio=0	
		
	def UpdateOnNewBars(self):
	# update the tracking when new bars are generated
		newprofit=self.position.GetCurrentProfit()
		self.returns.append(newprofit)

		
			
	def GetMetricData(self):
	# return data in terms of dict
		# Calculate Sharp Ratio
		nreturns=np.array(self.returns)
		sharp=(nreturns.mean()-0.0)/nreturns.std()
		
		Metrics={}
		Metrics['SharpRatio']=sharp
		return Metrics
	
	
class KMetricDrawDown(KBaseMetric):
	# calcuate maxium drawdown
	def __init__(self,parent):
		KBaseMetric.__init__(self,parent)
		self.maxdownratio=0.0
		self.maxdownduration=0
		self.curdownduration=0

	def UpdateOnNewBars(self):
	# update the tracking when new bars are generated
		newprofit=self.position.GetCurrentProfit()
		
		# update down ratio
		if self.maxdownratio>newprofit:
			self.maxdownratio=newprofit
		
		# update current down duration
		if newprofit>=0:
			self.curdownduration=0 # clear out
		else:
			self.curdownduration+=1 # accumulate
			
		# update max down duration
		if self.maxdownduration<self.curdownduration:
			self.maxdownduration=self.curdownduration
			
	def GetMetricData(self):
	# return data in terms of dict
		Metrics={}
		Metrics['MaxDown']=self.maxdownratio
		Metrics['MaxDownDuration']=self.maxdownduration
		return Metrics
			



